#include "ScriptSystem.h"
#include "common.h"
#include "Animation.h"
//#include "ui.h"
#include "gamemanager.h"
#include "inputmanager.h"

CScriptSystem::CScriptSystem()
{
    // push functions to lua
    L = lua_open();
    //luaopen_base(L);
    luaL_openlibs(L);

    lua_tinker::register_yield(L);//see func declare
    lua_tinker::register_package(L); 
   // newarray(L,100,"b");
    Initialize();
}

CScriptSystem::~CScriptSystem()
{
    lua_close(L);
}

void CScriptSystem::DoFile( const char* name )
{
    return lua_tinker::dofile(L,name);
}

void CScriptSystem::Initialize()
{
    DoFile( "config.lua" );
}


void CScriptSystem::ExposeCppToLua()
{
    Script.RegisteClass<IRect>("IRect");
    Script.RegisteMemberVariable<IRect>("x1",&IRect::x1);
    Script.RegisteMemberVariable<IRect>("y1",&IRect::y1);
    Script.RegisteMemberVariable<IRect>("x2",&IRect::x2);
    Script.RegisteMemberVariable<IRect>("y2",&IRect::y2);

    Script.RegisteClass<AnimationInfo>("AnimationInfo");
    Script.RegisteConstruction<AnimationInfo, const char*>();
    Script.RegisteMemberFunc<AnimationInfo>("AddKeyFrameSet",&AnimationInfo::AddKeyFrameSet);
    Script.RegisteClass<KeyFrameSetInfo>("KeyFrameSetInfo");
    Script.RegisteConstruction<KeyFrameSetInfo>();
    Script.RegisteMemberVariable<KeyFrameSetInfo>("direction",&KeyFrameSetInfo::direction);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("colorkey",&KeyFrameSetInfo::colorkey);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("framecount",&KeyFrameSetInfo::framecount);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("frametime",&KeyFrameSetInfo::frametime);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("startv",&KeyFrameSetInfo::startv);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("endv",&KeyFrameSetInfo::endv);
    Script.RegisteMemberVariable<KeyFrameSetInfo>("animmode",&KeyFrameSetInfo::animmode);
    Script.RegisteMemberFunc<KeyFrameSetInfo>("SetAnimName",&KeyFrameSetInfo::SetAnimName);
    Script.RegisteMemberFunc<KeyFrameSetInfo>("SetTextureName",&KeyFrameSetInfo::SetTextureName);
    Script.RegisteGlobalValue("Loop",eAnim_Loop);
    Script.RegisteGlobalValue("Once",eAnim_Once);

	Script.RegisteGlobalFunc("AddAnimInfo", AddAnimInfo);
	
	//Script.RegisteClass<Control>("Control");
	//Script.RegisteConstruction<Control>();
	//Script.RegisteMemberVariable<Control>("id",&Control::id);
	//Script.RegisteMemberVariable<Control>("text",&Control::text);
	//Script.RegisteMemberVariable<Control>("type",&Control::type);
	//Script.RegisteMemberVariable<Control>("x",&Control::x);
	//Script.RegisteMemberVariable<Control>("y",&Control::y);
	//Script.RegisteMemberVariable<Control>("width",&Control::width);
	//Script.RegisteMemberVariable<Control>("height",&Control::height);

	//Script.RegisteGlobalValue("Ctrl_Button",eCtrl_Button);
 //   Script.RegisteGlobalValue("Ctrl_Static",eCtrl_Static);
	//Script.RegisteClass<UIGadget>("UIGadget");
	//Script.RegisteMemberFunc<UIGadget>("AddControl",&UIGadget::AddControl);
	//Script.RegisteMemberFunc<UIGadget>("SetPosition",&UIGadget::SetPosition);
	//Script.RegisteMemberFunc<UIGadget>("SetSize",&UIGadget::SetSize);
	//Script.RegisteMemberFunc<UIGadget>("SetPosition",&UIGadget::SetPosition);
 //   Script.RegisteMemberFunc<UIGadget>("SetBackground",&UIGadget::SetBackground);
 //   Script.RegisteMemberFunc<UIGadget>("SetVisible",&UIGadget::SetVisible);
 //   Script.RegisteMemberFunc<UIGadget>("SetControlTexture",&UIGadget::SetControlTexture);
 //   Script.RegisteMemberFunc<UIGadget>("SetControlFont",&UIGadget::SetControlFont);
	//
	//Script.RegisteClass<UI>("UI");
	//Script.RegisteGlobalValue("UIMgr",&UI::Instance());
	//Script.RegisteMemberFunc<UI>("CreateGadget",&UI::CreateGadget);
 //   Script.RegisteMemberFunc<UI>("GetGadget",&UI::GetGadget);

	Script.RegisteClass<CGameManager>("CGameManager");
	Script.RegisteGlobalValue("Game",&CGameManager::Instance());
	Script.RegisteMemberFunc<CGameManager>("ExitGame",&CGameManager::ExitGame);
	
	Script.RegisteGlobalValue("eGame_Menu",eGame_Menu);
	Script.RegisteGlobalValue("eGame_Play",eGame_Play);
	Script.RegisteClass<GlobalContext>("GlobalContext");
	Script.RegisteGlobalValue("gc",&gc);
	Script.RegisteMemberVariable<GlobalContext>("gamestate",&GlobalContext::gamestate);
    Script.RegisteMemberVariable<GlobalContext>("screenrect",&GlobalContext::screenrect);

//    Script.RegisteClass<CDXUTElement>("ControlElement");

    Script.RegisteClass<CInputManager>("CInputManager");
    Script.RegisteGlobalValue("InputInfo",&CInputManager::Instance());
    Script.RegisteMemberFunc<CInputManager>("IsKeyPressed",&CInputManager::IsKeyPressed);
    Script.RegisteMemberFunc<CInputManager>("IsKeyPressing",&CInputManager::IsKeyPressing);
    Script.RegisteGlobalValue("KEY_ESCAPE",DIK_ESCAPE);
    Script.RegisteGlobalValue("DIK_LBUTTON",DIK_LBUTTON);
    Script.RegisteGlobalValue("DIK_RBUTTON",DIK_RBUTTON);
    Script.RegisteGlobalValue("DIK_SPACE",DIK_SPACE);
    Script.RegisteGlobalValue("DIK_RETURN",DIK_RETURN);

	//Script.RegisteGlobalValue("EVENT_BUTTON_CLICKED",EVENT_BUTTON_CLICKED);
	//Script.RegisteGlobalValue("EVENT_COMBOBOX_SELECTION_CHANGED",EVENT_COMBOBOX_SELECTION_CHANGED);
	//Script.RegisteGlobalValue("EVENT_RADIOBUTTON_CHANGED",EVENT_RADIOBUTTON_CHANGED);
	//Script.RegisteGlobalValue("EVENT_CHECKBOX_CHANGED",EVENT_CHECKBOX_CHANGED);
	//Script.RegisteGlobalValue("EVENT_SLIDER_VALUE_CHANGED",EVENT_SLIDER_VALUE_CHANGED);
	//Script.RegisteGlobalValue("EVENT_EDITBOX_STRING",EVENT_EDITBOX_STRING);
	//Script.RegisteGlobalValue("EVENT_EDITBOX_CHANGE",EVENT_EDITBOX_CHANGE);
	//Script.RegisteGlobalValue("EVENT_LISTBOX_ITEM_DBLCLK",EVENT_LISTBOX_ITEM_DBLCLK);
	//Script.RegisteGlobalValue("EVENT_LISTBOX_SELECTION",EVENT_LISTBOX_SELECTION);
	//Script.RegisteGlobalValue("EVENT_LISTBOX_SELECTION_END",EVENT_LISTBOX_SELECTION_END);

    Script.RegisteGlobalFunc( "L", MBToWChar );
}